/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.Reservation_statusDAO;
import com.core.database.Manager;
import com.core.entities.Reservation_status;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class Reservation_statusDAOImpl implements Reservation_statusDAO {
	Reservation_status reservation_status = new Reservation_status();
	ArrayList<Reservation_status> reservation_statusesList = new ArrayList<Reservation_status>();


	
	Connection connection = null;
	private static Reservation_statusDAOImpl instance=null;
	public static Reservation_statusDAOImpl getInstance(){
		if(instance==null){
			instance=new Reservation_statusDAOImpl();
		}
		return instance;
	}

	@Override
	public Reservation_status findByName(String name) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM reservation_status WHERE name = '"
					+ name + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					reservation_status.setId(rs.getInt(1));
					reservation_status.setName(rs.getString(2));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return reservation_status;
	}

	@Override
	public void insert(Reservation_status o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO reservation_status (name) VALUE (?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Reservation_status ", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Reservation_status findById(int id) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM reservation_status WHERE id = '" + id
					+ "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					reservation_status.setId(rs.getInt(1));
					reservation_status.setName(rs.getString(2));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return reservation_status;
	}

	@Override
	public List<Reservation_status> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM reservation_status ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				reservation_status.setId(rs.getInt(1));
				reservation_status.setName(rs.getString(2));

				reservation_statusesList.add(reservation_status);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return reservation_statusesList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM reservation_status WHERE id = '" + id
					+ "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, reservation_status.getName());

			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Reservation_status o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "UPDATE reservation_status SET name = '" + o.getName()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
